package com.ef.web.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.Classroom;
import com.ef.domain.School;
import com.ef.service.ClassroomService;
import com.ef.service.SchoolService;
import com.ef.util.CustomExample;

public class ClassroomAction extends BaseAction<Classroom> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ClassroomAction.class);

	private ClassroomService classroomService;

	private List<School> schoolList;

	@SuppressWarnings("serial")
	public String list() {
		final School school = this.getSessionUser().getSchool();
		this.listResult = classroomService.findByExample(
				new CustomExample<Classroom>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						if (school!=null){
							criteria.add(Restrictions.eq("school", school));
						}
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	private SchoolService schoolService;

	public String edit() {
		fetchListData();
		return EDIT;
	}

	private void fetchListData() {

		schoolList = schoolService.getAllValid();

	}

	public String save() {
		Classroom classroom = this.getModel();
		classroom.setUpdateTime(new Date());
		classroom.setUpdateUser(getSessionUser());
		if (classroom.getSchool() == null
				|| classroom.getSchool().getId() == null) {
			classroom.setSchool(null);
		}
		classroomService.saveOrUpdate(classroom);
		fetchListData();
		this.setResult(ConfirmMessage.CLASSROOM);
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			classroomService.delete(classroomService.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public ClassroomService getClassroomService() {
		return classroomService;
	}

	public void setClassroomService(ClassroomService classroomService) {
		this.classroomService = classroomService;
	}

	public String autoCompleteAjax() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String q = request.getParameter("q");
		String limit = request.getParameter("limit");
		School school = this.getSessionUser().getSchool();
		List<Classroom> list = null;
		if (school != null) {
			list = classroomService.findByNameInSchool(q, school.getId(), Integer
					.valueOf(limit));
		} else {
			list = classroomService.findByName(q, Integer.valueOf(limit));
		}
		// [{id:id;name:name},{id:id;name:name}]
		String responseStr = "";
		// System.out.println(responseStr);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			for (Classroom key : list) {
				responseStr = ("{id:") + (key.getId()) + (",") + ("name:\"")
						+ (key.getName()) + ("\",") + ("capacity:")
						+ (key.getCapacity()) + ("}");
				// System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}

	public SchoolService getSchoolService() {
		return schoolService;
	}

	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	public List<School> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(List<School> schoolList) {
		this.schoolList = schoolList;
	}
}
